using System.Collections;
using System.ComponentModel;

namespace Common.Core
{
	public class ValueResolver
	{
		readonly object source;
		readonly string name;

		public ValueResolver( object source, string name )
		{
			Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNull( source, "source" );
			Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNullOrEmpty( name, "name" );
			this.source = source;
			this.name = name;
		}

		public object Resolve()
		{
			IDictionary dictionary = source as IDictionary;
			if ( dictionary != null && dictionary.Contains( name ) )
			{
				object value = dictionary[ name ];
				return value;
			}
			PropertyDescriptor descriptor = TypeDescriptor.GetProperties( source ).Find( name, true );
			if ( descriptor != null )
			{
				object result = descriptor.GetValue( source );
				return result;
			}
			return source;
		}
	}
}